<script type="text/javascript" src="js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/ckfinder/ckfinder.js"></script>

<?php
	$id=$_GET['id'];
	//Lay thong tin hien tai "SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t2.category_id='".$idCategory."'"
	$sql="SELECT * FROM app t1 INNER JOIN category_app t2 ON t1.id = t2.app_id where t2.app_id='".$id."'";
	$rs=mysqli_query($conn,$sql);
	$r=mysqli_fetch_assoc($rs);
	
	if(count($_POST)>0)
	{
		$idCategory=$_POST['idCategory'];
		$name=$_POST['name'];
		if(!empty($_POST['description'])) $description=$_POST['description'];
		else $description='';
		$link=$_POST['link'];
		$AnHien=$_POST['AnHien'];
		
		$file=$_FILES['UrlHinh'];
		$UrlHinh=$r['image'];
		//Xu ly upload file
		if($file['name']!='')//Neu muon thay doi file
		{
			//Lay ten cua file de dua vo csdl
			$UrlHinh=$file['name'];			
			//Copy file tu thu muc temp -> thu muc chua file
			if(move_uploaded_file($file['tmp_name'],'img/app/'.$UrlHinh))//Neu copy thành công
			//Xoa hinh cũ khỏi hệ thống, dua vao thong tin trong CSDL
			unlink('img/app/'.$r['UrlHinh']);
		}
		//insert du lieu vao csdl
		
		$sql="update `app` set 	
		`name`='$name',
		`description`='$description',
		`image`='$UrlHinh',
		`link`='$link',
		`show_hide`='$AnHien'
		where `id`=$id";
		mysqli_query($conn,$sql);
		header('location:?mod=app&idCategory='.$idCategory);
	}
	
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <table width="400" border="1" style="width:700px;margin:auto">
    <caption>
      EDIT APP
    </caption>
      <a href ="?mod=app">Back to APP</a>
    <tr>
      <th width="171" scope="row">Loại</th>
      <td width="513"><select name="idCategory" id="idCategory">
        <?php
		//$idLoai=round($_GET['idLoai']);
		$sql2='select * from `category`';
		$rs2=mysqli_query($conn,$sql2);
		while($r2=mysqli_fetch_assoc($rs2))
		{
	?>
        <option <?php if($r2['id']==$r['category_id']) echo 'selected' ?> value="<?php echo $r2['id']?>"><?php echo $r2['name']?></option>
        <?php	
        }
	?>
      </select></td>
    </tr>
    <tr>
      <th width="171" scope="row">Name</th>
      <td><label>
        <input name="name" value="<?php echo $r['name']?>" type="text" id="name" size="40">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Description</th>
      <td><label>
        <textarea class="ckeditor" name="description" id="description" cols="30" rows="5"><?php echo $r['description']?></textarea>
      </label>
      </td>
    </tr>
    <tr>
      <th scope="row">Image</th>
      <td>
      <img src="img/app/<?php echo $r['image']?>" width="80" alt="Ảnh SP" />
      <br />
      <label>
        <input type="file" name="UrlHinh" id="UrlHinh">
      </label>
      <em><br />
      (Để trống nếu không muốn thay đổi) </em></td>
    </tr>
    <tr>
      <th scope="row">Link</th>
      <td><label>
        <input type="text" name="link" value="<?php echo $r['link']?>" id="link">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Ẩn/Hiện</th>
      <td><p>
        <label>
          <input name="AnHien" <?php if($r['show_hide']==1) echo 'checked'?> type="radio" id="AnHien_0" value="1" >
          Hiện</label>
        <br>
        <label>
          <input type="radio"  <?php if($r['show_hide']==0) echo 'checked'?> name="AnHien" value="0" id="AnHien_1">
          Ẩn</label>
        <br>
      </p></td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <td><label>
        <input type="reset" name="reset" id="reset" value="Nhập lại">
        <input type="submit" name="submit" id="submit" value="Cập nhật">
      </label></td>
    </tr>
  </table>
</form>
<script>
	  	/*var MoTa=CKEDITOR.replace( 'MoTa',
		{
			// Load the German interface.
			language: 'vi',
			skin:'office2003'
		});
		CKFinder.setupCKEditor( MoTa, 'js/ckfinder/' ) ;*/
		CKEDITOR.config.sharedSpaces =
		{
			top : 'toolbar',
			bottom : 'elementpath'
		};
</script>